/*
 * test_threads.cpp
 *
 *  Created on: 17.12.2012
 *      Author: aska
 */
#include "test_threads.h"

//pthread_create(&new_thread_data, 0, &func, &args);

test_1_thread::test_1_thread(void)
{
	mutex_command_test_1 = PTHREAD_MUTEX_INITIALIZER;
	command = no_command;
	state_timer = 0;
	state_hopper = 0;
	state_door_control = 0;
	cycle_counter = 0;
	sleep_delay = 250;
	test_state = 0;
	test_timer = 0;
	test_period = 8;
	test_counter = 0;
	test_is_running = 0;
	//test_2_is_running = 0;

}
test_1_thread::~test_1_thread()
{
}

void test_1_thread::run()
{
	test_proc((void*)15);
	cout << "::run" << endl;
}

int test_1_thread::log_add_str(char * log_str)
{
	int ret_val = 0;
	if (log_file_ptr != NULL)
	{
		char formatted_time[63];
		time_t rawtime;
		struct tm * timeinfo;
		time ( &rawtime );
		timeinfo = localtime ( &rawtime );
		log_file_year_day_prev = log_file_year_day_pres;
		//log_file_year_day_pres = timeinfo->tm_yday;
		log_file_year_day_pres = timeinfo->tm_min;
		if (log_file_year_day_prev != log_file_year_day_pres)
		{
			//printf("next log file\n");
			sprintf(log_file_name, "%sdoor_control_test_log_%4d_%2d_%2d.log", log_file_path, timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday);
			//printf("new log file name: %s\n", log_file_name);
			fclose(log_file_ptr);
			log_file_ptr = fopen(log_file_name, "a+");
		}
		//strftime (formatted_time, 63, "%Y.%m.%d %X",timeinfo);
		strftime (formatted_time, 63, "%X",timeinfo);
		fprintf(log_file_ptr, "%s : %s\n", formatted_time, log_str);
		fflush(log_file_ptr);
	}
	return 0;
}
int test_1_thread::log_add_timestamp()
{
	int ret_val = 0;
	if (log_file_ptr != NULL)
	{
		char formatted_time[63];
		time_t rawtime;
		struct tm * timeinfo;
		time ( &rawtime );
		timeinfo = localtime ( &rawtime );
		log_file_year_day_prev = log_file_year_day_pres;
		//log_file_year_day_pres = timeinfo->tm_yday;
		log_file_year_day_pres = timeinfo->tm_min;
		if (log_file_year_day_prev != log_file_year_day_pres)
		{
			//printf("next log file\n");
			sprintf(log_file_name, "%sdoor_control_test_log_%4d_%2d_%2d.log", log_file_path, timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday);
			//printf("new log file name: %s\n", log_file_name);
			fclose(log_file_ptr);
			log_file_ptr = fopen(log_file_name, "a+");
		}
		//strftime (formatted_time, 63, "%Y.%m.%d %X",timeinfo);
		strftime (formatted_time, 63, "%X",timeinfo);
		fprintf(log_file_ptr, "%s :", formatted_time);
		fflush(log_file_ptr);
	}
	return 0;
}


void * test_1_thread::test_proc(void *threadid)
{
	long tid;
	tid = (long)threadid;
	printf("thread #%ld!\n", tid);
	test_is_running = 1;
	int timer_time = 0;
	int buf_int = 0;
	char buf_str[32];
	int res_val = 0;
	log_file_year_day_prev = 0;
	log_file_year_day_pres = 0;
	strcpy(log_file_path, "");
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	log_file_year_day_prev = log_file_year_day_pres;
	//log_file_year_day_pres = timeinfo->tm_yday;
	log_file_year_day_pres = timeinfo->tm_min;
	sprintf(log_file_name, "%sdoor_control_test_log_%4d_%02d_%02d.log", log_file_path, timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday);
	//printf("log file name: %s\n", log_file_name);
	log_file_ptr = fopen(log_file_name, "a+");

	log_add_str("\n--test_1-------------------------");
	while (command != quit)
	{
		cycle_counter++;
		Sleep(sleep_delay);
		time ( &rawtime );
		timeinfo = localtime ( &rawtime );
		printf("\r%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
		pthread_mutex_lock(&mutex_command_test_1);
		if (state_timer == 1)
		{
			timer_time++;
		}
		pthread_mutex_unlock(&mutex_command_test_1);
		if (test_state == 1)
		{
			test_timer++;
			if (test_timer == test_period)
				test_timer = 0;
			if (test_timer == 0)
			{
				test_counter++;
				log_add_timestamp();
				sprintf(buf_str, " cyc %05d", test_counter);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				sprintf(buf_str, " init");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_init();
				sprintf(buf_str, " %d", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				Sleep(50);
				sprintf(buf_str, " param");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_ser_set_name(ser_name.c_str());
				res_val = door_control_lib_inst->door_control_ser_set_baud(ser_baud);
				sprintf(buf_str, " %d", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				sprintf(buf_str, " d_op");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_dev_open();
				sprintf(buf_str, " %d", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				//Sleep(50);
				sprintf(buf_str, " inp");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_inps_get();
				sprintf(buf_str, " %d", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				//********
				sprintf(buf_str, " m_res");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_modem_reset();
				sprintf(buf_str, " %d", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				//Sleep(50);
				sprintf(buf_str, " l_op");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_lock_set(1);
				sprintf(buf_str, " %d", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				Sleep(2000);
				sprintf(buf_str, " l_cl");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_lock_set(0);
				sprintf(buf_str, " %d", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				Sleep(2000);
				sprintf(buf_str, " d_fin");
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				res_val = door_control_lib_inst->door_control_finish();
				sprintf(buf_str, " %d\n", res_val);
				printf("%s", buf_str); fprintf(log_file_ptr, "%s", buf_str); fflush(log_file_ptr);
				//door_control_finish();
			}
		}
	}
	printf("\n");
	fclose(log_file_ptr);
	test_is_running = 0;
	pthread_exit(0);
}
/*
int test_thread::thread_start(int inp_arg)
{
    //rc = pthread_create(&run_thread_print_hello, 0, print_hello, (void *)t);
    rc = pthread_create(&thread_inst, NULL, test_thread::thread_start, (void*)this);
	return rc;
}*/
